package com.example.gisapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import com.esri.arcgisruntime.arcgisservices.ArcGISMapServiceInfo;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.layers.ArcGISMapImageSublayer;
import com.esri.arcgisruntime.layers.SublayerList;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.DrawStatusChangedEvent;
import com.esri.arcgisruntime.mapping.view.DrawStatusChangedListener;
import com.esri.arcgisruntime.mapping.view.MapView;

//加载基础底图
public class MapImageLayersActivity extends AppCompatActivity implements View.OnClickListener {
    MapView mapView;
    ArcGISMap mArcGISMap;
    DrawerLayout drawerLayout;
    private ArcGISMapImageLayer mMapImageLayer;

    private String url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer";

    private String url2 = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0";

    private SublayerList mSublayersList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setTitle("加载动态图层的子图层");
        setContentView(R.layout.activity_map_image_layers);
        drawerLayout = findViewById(R.id.drawerLayout);
        findViewById(R.id.tv1).setOnClickListener(this);
        findViewById(R.id.tv2).setOnClickListener(this);
        findViewById(R.id.tv3).setOnClickListener(this);


        mapView = findViewById(R.id.mapView);

        //底图
        mArcGISMap = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 47.6047381, -122.3334255, 12);

        //动态图层
        mMapImageLayer = new ArcGISMapImageLayer(url);
        mMapImageLayer.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                //获取所有子图层的集合
                mSublayersList = mMapImageLayer.getSublayers();
                Log.e("xyh", "onCreate: " + mSublayersList.size());
                ArcGISMapImageSublayer mainMapImageSublayer = (ArcGISMapImageSublayer) mSublayersList.get(0);
                ArcGISMapServiceInfo mapServiceInfo = mMapImageLayer.getMapServiceInfo();
            }
        });

        //设置动态图层的透明度
        mMapImageLayer.setOpacity(0.5f);


        //默认会把所以子图层都显示出来
        mArcGISMap.getOperationalLayers().add(mMapImageLayer);
        //可以设置所有子图层都不显示
        //        for (int i = 0; i < mSublayersList.size(); i++) {
        //            mSublayersList.get(i).setVisible(false);
        //        }
        mapView.setMap(mArcGISMap);


        //监听图层的视图状态
        mapView.addDrawStatusChangedListener(new DrawStatusChangedListener() {
            @Override
            public void drawStatusChanged(DrawStatusChangedEvent drawStatusChangedEvent) {
                Log.d("drawStatusChanged", "drawStatusChangedEvent.getDrawStatus: " + drawStatusChangedEvent.getDrawStatus());
            }
        });

    }

    @Override
    public void onClick(View v) {
        TextView tv = (TextView) v;
        boolean isSelect = !tv.isSelected();
        tv.setSelected(isSelect);
        tv.setTextColor(isSelect ? Color.RED : Color.BLACK);
        switch (v.getId()) {
            case R.id.tv1:
                mSublayersList.get(0).setVisible(isSelect);
                break;
            case R.id.tv2:
                mSublayersList.get(1).setVisible(isSelect);
                break;
            case R.id.tv3:
                mSublayersList.get(2).setVisible(isSelect);
                break;
        }
        drawerLayout.closeDrawer(Gravity.LEFT);
    }


    @Override
    protected void onPause() {
        mapView.pause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mapView.resume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mapView.dispose();
        super.onDestroy();
    }

}